package com.hua.api.core.framework.http.request;

import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.framework.http.HttpHelper;
import com.hua.api.core.framework.http.exception.SgOpenException;
import com.hua.api.core.framework.http.response.OpenApiResponse;
import com.hua.api.core.framework.http.util.ConvertUtil;
import com.hua.api.core.framework.util.Result;
import com.hua.api.core.framework.util.exception.ApiError;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;

import java.lang.reflect.Field;
import java.util.*;


@Slf4j
@SuppressWarnings("all")
public class OpenApiRequest {

    public String uri;
    public String requestMethodType;
    private boolean postJson = false;

    protected static Set<Class> BASIC_PARAM_TYPE_SET = new HashSet();

    static {
        BASIC_PARAM_TYPE_SET.add(Boolean.TYPE);
        BASIC_PARAM_TYPE_SET.add(Boolean.class);
        BASIC_PARAM_TYPE_SET.add(Integer.TYPE);
        BASIC_PARAM_TYPE_SET.add(Integer.class);
        BASIC_PARAM_TYPE_SET.add(Long.TYPE);
        BASIC_PARAM_TYPE_SET.add(Long.class);
        BASIC_PARAM_TYPE_SET.add(Float.TYPE);
        BASIC_PARAM_TYPE_SET.add(Float.class);
        BASIC_PARAM_TYPE_SET.add(Double.TYPE);
        BASIC_PARAM_TYPE_SET.add(Double.class);
        BASIC_PARAM_TYPE_SET.add(String.class);
    }


    public Map<String, String> constructApplicationParamMap() throws SgOpenException {
        try {
            Map<String, String> paramMap = new HashMap();
            Field[] declaredFields = this.getClass().getDeclaredFields();
            if (declaredFields != null && declaredFields.length != 0) {
                Field[] var3 = declaredFields;
                int var4 = declaredFields.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    Field declaredField = var3[var5];
                    declaredField.setAccessible(true);
                    Class typeClass = declaredField.getType();
                    String paramName = declaredField.getName();
                    String paramValue = null;
                    if (BASIC_PARAM_TYPE_SET.contains(typeClass)) {
                        paramValue = String.valueOf(declaredField.get(this));
                        if (paramValue != null && !"".equals(paramValue) && !"null".equals(paramValue) && !"NULL".equals(paramValue)) {
                            paramMap.put(paramName, paramValue);
                        }
                    } else {
                        Map<String, String> objectMap = ConvertUtil.convertToMap(declaredField.get(this));
                        paramMap.putAll(objectMap);
                    }

                    if (paramValue != null && !"".equals(paramValue) && !"null".equals(paramValue) && !"NULL".equals(paramValue)) {
                        paramMap.put(paramName, paramValue);
                    }
                }

                return paramMap;
            } else {
                return paramMap;
            }
        } catch (Exception var11) {
            var11.printStackTrace();
            throw new SgOpenException(var11);
        }
    }

    public OpenApiRequest(String uri, String requestMethodType,boolean postJson) {
        this.uri = uri;
        this.requestMethodType = requestMethodType;
        this.postJson = postJson;
    }

    public OpenApiRequest(String uri, String requestMethodType) {
        this.uri = uri;
        this.requestMethodType = requestMethodType;
    }

    public String getBody(){
        return new JSONObject().toJSONString();
    }

    public String createUrl(){
        return this.uri;
    }

    public Map<String,String> getHeaders(){
        return new HashMap<>();
    }

    public void logParams(Map<String, String> params){

    }

    public OpenApiResponse doRequest() {
        try {
            Map<String, String> params = this.constructApplicationParamMap();
            String url = createUrl();
            Map<String,String> headers = getHeaders();

            for(Map.Entry<String,String> en:params.entrySet()){
                System.out.println(en.getKey()+"="+en.getValue());
            }

            if(this.postJson){
                String body = getBody();
                String resultString = HttpHelper.postJson(url,body,headers,RequestConfig.custom());
                return OpenApiResponse.of(resultString);
            }else {
                logParams(params);
                String resultString = HttpHelper.request(url,params,headers,requestMethodType,RequestConfig.custom());
                return OpenApiResponse.of(resultString);
            }

        } catch (Exception e1){
            return OpenApiResponse.error(e1.getMessage());
        }
    }


    public Result getResult(){
        OpenApiResponse response = doRequest();
        if(response.isError){
            return Result.error(ApiError.Type.LOGIC_ERROR.toException(response.errMsg));
        }else {
            return Result.of(response.toJSONObject());
        }
    }



}
